﻿namespace DepartmentSpecificStoreView
{
    using System;
    using System.Collections.Generic;
    using System.Data;

    using Microsoft.SharePoint;

    public class TargetFieldsDataCollectionBL
    {
        #region Fields

        List<DepartmentVisibleFields> _AvailableFields;

        #endregion Fields

        #region Properties

        public List<DepartmentVisibleFields> AvailableFields
        {
            get
            {
                if ( _AvailableFields == null )
                    _AvailableFields = new List<DepartmentVisibleFields>( );
                return _AvailableFields;
            }
        }

        #endregion Properties

        #region Methods

        public static void DeleteSelectedField( SPWeb storeWeb, string sourceDepartment, string targetDepartment, string fieldName )
        {
            SPList departmentFieldsView = storeWeb.Lists[ "Department Access Administration" ];
            SPQuery sourceUserDepartments = new SPQuery( );

            sourceUserDepartments.Query = "<Where><And><Eq><FieldRef Name='Department_x0020_Name' /><Value Type='Text'>" + sourceDepartment + "</Value></Eq><And><Eq><FieldRef Name='Target_x0020_List_x0020_Name' /><Value Type='Text'>" + targetDepartment + "</Value></Eq><Eq><FieldRef Name='Field_x0020_Name' /><Value Type='Text'>" + fieldName + "</Value></Eq></And></And></Where>";
            sourceUserDepartments.ViewFields = "<FieldRef Name='ID'/>";

            SPListItemCollection allFieldViews = departmentFieldsView.GetItems( sourceUserDepartments );
            if ( allFieldViews.Count == 0 )
                return;
            allFieldViews[ 0 ].Delete( );
            allFieldViews[ 0 ].Update( );
        }

        public static void SaveSelectedField( SPWeb storeWeb, string sourceDepartment, string targetDepartment, string targetList, string fieldName )
        {
            SPListItem item;
            SPList departmentFieldsView = storeWeb.Lists[ "Department Access Administration" ];
            item = departmentFieldsView.Items.Add( );
            item[ "Title" ] = sourceDepartment;
            item[ "Department Name" ] = sourceDepartment;
            item[ "Target List Name" ] = targetList;
            item[ "Field Name" ] = fieldName;
            item[ "Target Department Name" ] = targetDepartment;
            item.Update( );
            departmentFieldsView.Update( );
        }

        public DataTable GetAllBaseDepartmentSelectedFields( DataTable dt, SPWeb storeWeb, string listName )
        {
            SPList departmentFieldsView = storeWeb.Lists[ "Department Ordering Lookup" ];
            SPQuery sourceUserDepartments = new SPQuery( );

            sourceUserDepartments.Query = "<Where><Eq><FieldRef Name='Department_x0020_Owner' /><Value Type='Text'>" + listName + "</Value></Eq></Where><OrderBy><FieldRef Name='Order_x0020_Number' Ascending='True' /></OrderBy>";
            SPListItemCollection allFieldViews = departmentFieldsView.GetItems( sourceUserDepartments );

            string currentCode = "";

            foreach ( SPListItem departmentField in allFieldViews )
            {
                try
                {
                    if ( departmentField[ "Source Department" ].ToString( ).Trim( ).Equals( "Stores Detail" ) )
                        currentCode = "Base";
                    else
                    {
                        try
                        {
                            currentCode = GetDepartmentAbbriviations( storeWeb, departmentField[ "Source Department" ].ToString( ).Trim( ) );
                        }
                        catch
                        {
                            currentCode = "Error";
                        }
                    }
                    if ( departmentField[ "Hidden" ] == null )
                    {
                        if ( departmentField[ "FieldType" ].ToString( ).Equals( "DateTime" ) )
                            dt.Columns.Add( departmentField[ "Field Name" ].ToString( ).Trim( ), typeof( DateTime ) );
                        else
                            dt.Columns.Add( departmentField[ "Field Name" ].ToString( ).Trim( ), typeof( string ) );
                    }
                    else
                    {
                        if ( !bool.Parse( departmentField[ "Hidden" ].ToString( ) ) )
                        {
                            if ( departmentField[ "FieldType" ].ToString( ).Equals( "DateTime" ) )

                                dt.Columns.Add( departmentField[ "Field Name" ].ToString( ).Trim( ), typeof( DateTime ) );
                            else
                                dt.Columns.Add( departmentField[ "Field Name" ].ToString( ).Trim( ), typeof( string ) );
                        }
                    }
                }
                catch
                {
                    if ( departmentField[ "Hidden" ] == null )
                    {
                        if ( departmentField[ "FieldType" ].ToString( ).Equals( "DateTime" ) )

                            dt.Columns.Add( departmentField[ "Field Name" ].ToString( ).Trim( ) + " - " + currentCode, typeof( DateTime ) );
                        else
                            dt.Columns.Add( departmentField[ "Field Name" ].ToString( ).Trim( ) + " - " + currentCode, typeof( string ) );
                    }
                    else
                    {
                        if ( !bool.Parse( departmentField[ "Hidden" ].ToString( ) ) )
                        {
                            if ( departmentField[ "FieldType" ].ToString( ).Equals( "DateTime" ) )

                                dt.Columns.Add( departmentField[ "Field Name" ].ToString( ).Trim( ) + " - " + currentCode, typeof( DateTime ) );
                            else
                                dt.Columns.Add( departmentField[ "Field Name" ].ToString( ).Trim( ) + " - " + currentCode, typeof( string ) );
                        }
                    }
                }
            }

            return dt;
        }

        public void GetAllDepartmentSelectedFields( SPWeb storeWeb, string sourceDepartment )
        {
            DepartmentVisibleFields deptFields = new DepartmentVisibleFields( );

            SPList departmentFieldsView = storeWeb.Lists[ "Department Access Administration" ];
            SPQuery sourceUserDepartments = new SPQuery( );

            sourceUserDepartments.Query = "<Where><Eq><FieldRef Name='Department_x0020_Name' /><Value Type='Text'>" + sourceDepartment.Trim( ) + "</Value></Eq></Where><OrderBy><FieldRef Name='Target_x0020_List_x0020_Name' Ascending='True' /><FieldRef Name='Field_x0020_Name' Ascending='True' /></OrderBy>";
            sourceUserDepartments.ViewFields = "<FieldRef Name='Target_x0020_Department_x0020_Name'/><FieldRef Name='Field_x0020_Name'/><FieldRef Name='Target_x0020_List_x0020_Name'/>";
            SPListItemCollection allFieldViews = departmentFieldsView.GetItems( sourceUserDepartments );

            foreach ( SPItem item in allFieldViews )
            {
                deptFields = new DepartmentVisibleFields( );
                deptFields.SourceDepartmentName = sourceDepartment;
                deptFields.TargetDepartmentName = item[ "Target Department Name" ].ToString( ).Trim( );
                deptFields.TargetFieldName = item[ "Field Name" ].ToString( ).Trim( );
                deptFields.TargetListName = item[ "Target List Name" ].ToString( ).Trim( );
                deptFields.ReadOnly = true;
                AvailableFields.Add( deptFields );
            }
        }

        public string GetDepartmentAbbriviations( SPWeb storeWeb, string sourceDepartment )
        {
            string result = "";
            SPList departmentListValues = storeWeb.Lists[ "Department Administration List" ];
            SPQuery nonUserDepartments = new SPQuery( );

            nonUserDepartments.Query = "<Where><Eq><FieldRef Name='Department_x0020_Name' /><Value Type='Text'>" + sourceDepartment.Trim( ) + "</Value>   </Eq></Where>";
            nonUserDepartments.ViewFields = "<FieldRef Name='Department_x0020_Grid_x0020_Code'/>";
            SPListItemCollection otherDepartments = departmentListValues.GetItems( nonUserDepartments );
            if ( otherDepartments.Count == 0 )
                return result;
            else
               return result = otherDepartments[ 0 ][ "Department Grid Code" ].ToString( ).Trim( );
        }

        public void GetSourceTargetDepartmentSelectedFields( SPWeb storeWeb, string sourceDepartment, string targetDepartment )
        {
            DepartmentVisibleFields deptFields = new DepartmentVisibleFields( );

            SPList departmentFieldsView = storeWeb.Lists[ "Department Access Administration" ];
            SPQuery sourceUserDepartments = new SPQuery( );

            sourceUserDepartments.Query = "<Where><And><Eq><FieldRef Name='Department_x0020_Name' /><Value Type='Text'>" + sourceDepartment + "</Value></Eq><Eq><FieldRef Name='Target_x0020_List_x0020_Name' /><Value Type='Text'>" + targetDepartment + "</Value></Eq></And></Where><OrderBy><FieldRef Name='Target_x0020_List_x0020_Name' Ascending='True' /><FieldRef Name='Field_x0020_Name' Ascending='True' /></OrderBy>";
            sourceUserDepartments.ViewFields = sourceUserDepartments.ViewFields = "<FieldRef Name='Target_x0020_Department_x0020_Name'/><FieldRef Name='Field_x0020_Name'/><FieldRef Name='Target_x0020_List_x0020_Name'/>";

            SPListItemCollection allFieldViews = departmentFieldsView.GetItems( sourceUserDepartments );

            foreach ( SPItem item in allFieldViews )
            {
                deptFields = new DepartmentVisibleFields( );
                deptFields.SourceDepartmentName = sourceDepartment;
                deptFields.TargetDepartmentName = item[ "Target Department Name" ].ToString( ).Trim( );
                deptFields.TargetFieldName = item[ "Field Name" ].ToString( ).Trim( );
                deptFields.TargetListName = item[ "Target List Name" ].ToString( ).Trim( );
                deptFields.ReadOnly = false;
                AvailableFields.Add( deptFields );
            }
        }

        #endregion Methods
    }
}